package com.example.testapplication;


import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wwq on 2019/7/29  10:40.
 */
public class QuestionDetailAdapter extends BaseQuickAdapter<QuestionListAnswer.DataBean.PageDataBean, BaseViewHolder> {
    QuestionDetailAdapter(int layoutResId, @Nullable List<QuestionListAnswer.DataBean.PageDataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, QuestionListAnswer.DataBean.PageDataBean item) {
        String url = "http://api.91jtg.com:8022/upload/images/" + item.getUserInfoDto().getHeadPicture();
        helper.setText(R.id.tvNickNmae, item.getUserInfoDto().getNickName())
                .setText(R.id.tvCertificateValue, item.getUserInfoDto().getCertificateValue())
                .setText(R.id.tvAnswer, item.getAnswer())
                .setText(R.id.tvCreateime, switchCreateTime(item.getCreateTime()))
                .setText(R.id.tvAgreeNum, "赞同 " + item.getAgreeNum())
                .setText(R.id.tvAddress,item.getProvinceValue() + " " + item.getRegionValue())
                .setGone(R.id.tvAdopt, item.isHasAccept())
                .addOnClickListener(R.id.tvFollow)
                .addOnClickListener(R.id.llAgree);
        Glide.with(mContext).load(url).into((ImageView) helper.getView(R.id.ivHeadPicture));
        if (item.isFocus()){
            helper.setText(R.id.tvFollow, "已关注");
        }
        else {
            helper.setText(R.id.tvFollow, "+ 关注");
        }
        if (item.isHasAgree()){
            helper.setImageResource(R.id.ivAgree, R.mipmap.tf_zt_selected);
        }
        else {
            helper.setImageResource(R.id.ivAgree, R.mipmap.tf_zt_default);
        }
    }


    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    private String switchCreateTime(String createTime){
        String str = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date time = format.parse(createTime);
            String date = time.toString();
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.US);
            Date datetime = (Date) sdf.parse(date);
            str = new SimpleDateFormat("MM-dd HH:mm").format(datetime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }
}
